package com.catsic.pub.drawer.dao;

import java.util.List;

import javax.persistence.QueryHint;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.QueryHints;

import com.catsic.base.BaseDao;
import com.catsic.pub.entity.Drawer;

public interface DrawerDao extends BaseDao<Drawer, String> {
	
	/**
	 * 通过父节点查询所有子节点
	 * @param id
	 * @return
	 */
	@Query("select d from Drawer d")
	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value ="true") })
	public List<Drawer> findAll();
	
	/**
	 * 通过父节点查询所有子节点
	 * @param id
	 * @return
	 */
	@Query("select d from Drawer d where d.parent.id = ?1 order by d.code asc")
	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value ="true") })
	public List<Drawer> findByPid(String id);
	
	
	/**
	 * 通过节点编码获取节点
	 * @param code
	 * @return
	 */
	//@Query("select d from Drawer d where d.code = ?1")
	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value ="true") })  
	public Drawer findByCode(String code);
	
	/**
	 * 通过父节点编码获取子节点
	 * @param code
	 * @return
	 */
	@Query("select cds from Drawer drawer left join drawer.childs cds where drawer.code = ?1 order by drawer.code asc")
	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value ="true") })  
	public List<Drawer> findByParentCode(String code);
	
	/**
	 * findByParentIdOrderByCodeAsc:查询父节点下所有子节点,按编码升序排列
	 * @author yangyd
	 * @param id
	 * @return
	 */
	public List<Drawer> findByParentIdOrderByCodeAsc(String id);
	
	/**
	 * findByCodeIsNotNullAndParentIdOrderByCodeDesc:查询父节点下所有编码不为空的子节点,按编码倒叙排列
	 * @author yangyudi
	 * @param id
	 * @return
	 */
	public List<Drawer> findByCodeIsNotNullAndParentIdOrderByCodeDesc(String id);
	
	/**
	 * 通过父节点和值获取子节点
	 * @param parentCode
	 * @param value
	 * @return
	 */
	@Query("select cds from Drawer drawer left join drawer.childs cds where drawer.code = ?1 and cds.value = ?2")
	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value ="true") })  
	public Drawer findByParentAndValue(String parentCode,String value);
}
